package com.ship.dispatch.bean;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ship.common.core.base.ContractBaseBean;
import com.ship.common.core.mapping.Label;
import com.ship.common.core.mapping.SearchItem;
import com.ship.common.core.mapping.TableColumn;
import com.ship.common.core.utils.DateUtil;
import com.ship.common.core.validate.Add;
import com.ship.common.core.validate.Update;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author 31463
 */
@TableName("con_resource_memorandum")
@Data
public class ConResourceMemorandum extends ContractBaseBean implements Serializable {

    private static final long serialVersionUID = 3409270939193648584L;
    /**
     * 合同编号
     */
    @Label(value = "合同编号")
    @TableColumn
    private String contractNumber;

    /**
     * 合同名称
     */
    @Label(value = "合同名称", multiSelect = true)
    @SearchItem
    @TableColumn
    private String contractName;

    @TableField(exist = false)
    private String contractNameListForSearch;

    /**
     * 关联主合同
     */
    @Label(value = "关联主合同", enumContent = "join:ConResourceMain")
    @SearchItem
    private Long mainContract;

    /**
     * 关联主合同名字
     */
    @Label(value = "关联主合同", multiSelect = true)
    @TableColumn
    private String mainContractName;

    @TableField(exist = false)
    private String mainContractNameListForSearch;

    /**
     * 贸易类型(1:长协、2:中期、3:现货)
     */
    @Label(value = "贸易类型", enumContent = "贸易类型", multiSelect = true)
    @SearchItem
    @TableColumn
    @NotNull(message = "贸易类型不能为空", groups = {Add.class, Update.class})
    private Integer tradeType;

    @TableField(exist = false)
    private String tradeTypeListForSearch;

    /**
     * 贸易主体(国贸、新贸)
     */
    @Label(value = "贸易主体", enumContent = "贸易主体", multiSelect = true)
    @SearchItem
    @TableColumn
    @NotNull(message = "贸易主体不能为空", groups = {Add.class, Update.class})
    private Integer tradeBody;

    @TableField(exist = false)
    private String tradeBodyListForSearch;

    /**
     * 贸易模式(采购、销售)
     */
    @Label(value = "贸易模式", enumContent = "贸易模式", multiSelect = true)
    @SearchItem
    @TableColumn
    @NotNull(message = "贸易模式不能为空", groups = {Add.class, Update.class})
    private Integer tradeModel;

    @TableField(exist = false)
    private String tradeModelListForSearch;

    /**
     * 签订日期
     */
    @Label(value = "签订日期")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @TableColumn
    @NotNull(message = "签订日期不能为空", groups = {Add.class, Update.class})
    private Date signingDate;

    /**
     * 买方
     */
    @Label(value = "买方", multiSelect = true)
    @SearchItem
    @TableColumn
    @NotBlank(message = "买方不能为空", groups = {Update.class, Add.class})
    @Size(max = 255, message = "买方长度不能超过255", groups = {Update.class, Add.class})
    private String buyer;

    @TableField(exist = false)
    private String buyerListForSearch;

    /**
     * 卖方
     */
    @Label(value = "卖方", multiSelect = true)
    @SearchItem
    @TableColumn
    @NotBlank(message = "卖方不能为空", groups = {Update.class, Add.class})
    @Size(max = 255, message = "卖方长度不能超过255", groups = {Update.class, Add.class})
    private String seller;

    @TableField(exist = false)
    private String sellerListForSearch;

    /**
     * 交货方式(FOB、DES)
     */
    @Label(value = "交货方式", enumContent = "交货方式", multiSelect = true)
    @SearchItem
    @TableColumn
    @NotNull(message = "交货方式不能为空", groups = {Add.class})
    private Integer deliveryType;

    @TableField(exist = false)
    private String deliveryTypeListForSearch;

    /**
     * 装载港
     */
    @Label(value = "装载港", multiSelect = true)
    @SearchItem
    @TableColumn
    @NotBlank(message = "装载港不能为空", groups = {Update.class, Add.class})
    @Size(max = 255, message = "装载港长度不能超过255", groups = {Update.class, Add.class})
    private String loadingPort;

    @TableField(exist = false)
    private String loadingPortListForSearch;

    /**
     * 卸载港
     */
    @Label(value = "卸载港", multiSelect = true)
    @SearchItem
    @TableColumn
    @NotBlank(message = "卸载港不能为空", groups = {Update.class, Add.class})
    @Size(max = 255, message = "卸载港长度不能超过255", groups = {Update.class, Add.class})
    private String unloadingPort;

    @TableField(exist = false)
    private String unloadingPortListForSearch;

    /**
     * 换装载港时间
     */
    @Label(value = "换港时间（装载港）")
    @TableColumn
    private String changePortTimeLoading;

    /**
     * 换卸载港时间
     */
    @Label(value = "换港时间（卸载港）")
    @TableColumn
    private String changePortTimeUnloading;

    /**
     * 交付窗口-开始
     */
    @Label(value = "交付窗口-开始", format = "YYYY-MM-DD HH:mm:ss")
    @NotNull(message = "交付窗口-开始不能为空", groups = {Add.class, Update.class})
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date deliveryWindowBegin;

    /**
     * 交付窗口-结束
     */
    @Label(value = "交付窗口-结束", format = "YYYY-MM-DD HH:mm:ss")
    @NotNull(message = "交付窗口-开始不能为空", groups = {Add.class, Update.class})
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date deliveryWindowEnd;

    /**
     * 合同装/卸载量
     */
    @Label(value = "装/卸载量")
    @NotNull(message = "装/卸载量不能为空", groups = {Update.class, Add.class})
    private BigDecimal loadingOrUnloadingCapacity;

    /**
     * 船舶名称
     */
    @Label(value = "船舶名称", multiSelect = true)
    @SearchItem
    @TableColumn
    private String shipName;

    @TableField(exist = false)
    private String shipNameListForSearch;

    /**
     * 允许装载时间(单位小时)
     */
    @Label(value = "允许装载时间")
    private Integer allowLoadingTime;

    /**
     * 允许装载时间(单位小时)
     */
    @Label(value = "允许装载时间")
    @TableColumn
    @TableField(exist = false)
    private String allowLoadingTimeStr;

    /**
     * 蒸发率(每天)
     */
    @Label(value = "蒸发率")
    private BigDecimal evaporationRate;

    /**
     * 蒸发率拼接值
     */
    @Label(value = "蒸发率")
    @TableColumn
    @TableField(exist = false)
    private String evaporationRateStr;

    /**
     * 滞期费率(每天)
     */
    @Label(value = "滞期费率")
    private BigDecimal demurrageRate;

    /**
     * 滞期费率(每天)
     */
    @Label(value = "滞期费率")
    @TableColumn
    @TableField(exist = false)
    private String demurrageRateStr;

    /**
     * 合同价格
     */
    @Label(value = "合同价格")
    private BigDecimal contractPrice;

    /**
     * 合同价格
     */
    @Label(value = "合同价格")
    @TableColumn
    @TableField(exist = false)
    private String contractPriceStr;

    /**
     * 计量条款
     */
    @Label(value = "计量条款")
    @TableColumn
    private String measurementClause;

    /**
     * IMO
     */
    @Label(value = "IMO", multiSelect = true)
    @SearchItem
    @TableColumn
    private String imo;

    @TableField(exist = false)
    private String imoListForSearch;

    /**
     * 换船时间
     */
    @Label(value = "换船时间")
    @TableColumn
    private String changeShipTime;

    /**
     * 状态（草稿、已签订、待执行、已完成）
     */
    @Label(value = "合同状态", enumContent = "合同状态")
    @TableColumn
    private Integer status;

    /**
     * 币种
     */
    @Label(value = "币种", enumContent = "币种")
    private Integer currency;

    /**
     * 删除申请
     */
    @Label(value = "删除申请", boolContent = true)
    private Boolean delApply;

    /**
     * 优秀案例
     */
    @Label(value = "优秀案例", enumContent = "是否是优秀案例")
    private Integer excellent;

    /**
     * 附件地址
     */
    @TableField(exist = false)
    private String filePath;

    /**
     * 合同上传时间
     */
    @Label(value = "合同上传时间")
    @TableColumn
    @TableField(fill = FieldFill.INSERT)
    private Date createDate;

    /**
     * 创建者
     */
    @Label(value = "创建者", multiSelect = true)
    @TableColumn
    @SearchItem
    @TableField(fill = FieldFill.INSERT)
    private String createBy;

    @TableField(exist = false)
    private String createByListForSearch;

    @Label(value = "交付窗口")
    @TableColumn
    @TableField(exist = false)
    private String deliveryWindow;

    @Label(value = "装/卸载量")
    @TableColumn
    @TableField(exist = false)
    private String loadingOrUnloadingCapacityStr;

    /**
     * 装/卸载量上浮值
     */
    @Label(value = "装/卸载量上浮值")
    private BigDecimal upFloatValue;

    /**
     * 装/卸载量下浮值
     */
    @Label(value = "装/卸载量下浮值")
    private BigDecimal downFloatValue;

    /**
     * 装卸载量单位
     */
    @Label(value = "装卸载量单位", enumContent = "装卸载量单位")
    private Integer unit;

    public String getDeliveryWindow() {
        String beginTime = DateUtil.date2Str(getDeliveryWindowBegin(), "yyyy年MM月dd日 HH:mm");
        String endTime = DateUtil.date2Str(getDeliveryWindowEnd(), "yyyy年MM月dd日 HH:mm");
        String beginDate = beginTime.substring(0, 11);
        String endDate = endTime.substring(0, 11);
        if (beginDate.equals(endDate)) {
            return beginDate + " " + beginTime.substring(12, beginTime.length()) + "-" + endTime.substring(12, beginTime.length());
        }
        return beginTime + "-" + endTime;
    }

    public String getLoadingOrUnloadingCapacityStr() {
        BigDecimal loadingOrUnloadingCapacity = getLoadingOrUnloadingCapacity();
        BigDecimal upFloatValue = getUpFloatValue();
        BigDecimal downFloatValue = getDownFloatValue();
        if (loadingOrUnloadingCapacity == null || upFloatValue == null || downFloatValue == null) {
            return null;
        }
//        String result = decimalFormat.format(numericFormatting(loadingOrUnloadingCapacity));
        if (upFloatValue.equals(downFloatValue)) {
            return decimalFormat.format(loadingOrUnloadingCapacity) + "±" + numericFormatting(upFloatValue) + "%";
        }
        return decimalFormat.format(loadingOrUnloadingCapacity) + "+" + numericFormatting(upFloatValue) + "%" + "-" + numericFormatting(downFloatValue) + "%";
    }


    public String getDemurrageRateStr() {
        String result = null;
        if (getDemurrageRate() != null) {
            String demurrageRate = numericFormatting(getDemurrageRate());
            if (currency == 1) {
                result = "$" + demurrageRate;
            }
            if (currency == 2) {
                result = "¥" + demurrageRate;
            }
            if (currency == 3) {
                result = "€" + demurrageRate;
            }
            if (currency == 4) {
                result = "￡" + demurrageRate;
            }
            if (currency == 5) {
                result = "C$" + demurrageRate;
            }
            if (currency == 6) {
                result = "￥" + demurrageRate;
            }
            if (currency == 7) {
                result = "₹" + demurrageRate;
            }
            if (currency == 8) {
                result = "p" + demurrageRate;
            }
            if (currency == 9) {
                result = "₽" + demurrageRate;
            }
        }
        return result;
    }

    public String getEvaporationRateStr() {
        String result = null;
        if (getEvaporationRate() != null) {
            BigDecimal EvaporationRate = getEvaporationRate();
            result = numericFormatting(EvaporationRate) + "%";
        }
        return result;
    }

    public String getContractPriceStr() {
        String result = null;
        if (getContractPrice() != null) {
            result = decimalFormat.format(getContractPrice());
            if (currency == 1) {
                result = "$" + result;
            }
            if (currency == 2) {
                result = "¥" + result;
            }
            if (currency == 3) {
                result = "€" + result;
            }
            if (currency == 4) {
                result = "￡" + result;
            }
            if (currency == 5) {
                result = "C$" + result;
            }
            if (currency == 6) {
                result = "￥" + result;
            }
            if (currency == 7) {
                result = "₹" + result;
            }
            if (currency == 8) {
                result = "p" + result;
            }
            if (currency == 9) {
                result = "₽" + result;
            }
        }
        return result;
    }

    public String getAllowLoadingTimeStr() {
        String result = null;
        if (getAllowLoadingTime() != null) {
            result = getAllowLoadingTime() + "小时";
        }
        return result;
    }
}
